package com.srpingde.hhh;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.MeiWenBean;
import com.srpingde.hhh.bean.MeiWenDataBean;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class MeiWenActivity extends Activity {
	Button collection_meiwen;
	TextView title,content_tv,title_name;
	String id;
	String cid = null;
	HHHIml weiQuIml;
	String type;
	String titlestr,pid,aid;
	Boolean hideShoucang;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mei_wen);
        collection_meiwen = (Button) findViewById(R.id.collection_meiwen);
        title = (TextView) findViewById(R.id.title);
        title_name = (TextView) findViewById(R.id.title_name);
        content_tv = (TextView) findViewById(R.id.content);
        weiQuIml = new HHHIml();
        if(getIntent().getExtras()!=null){
        	//id = getIntent().getStringExtra("id");
        	//pid =  getIntent().getStringExtra("pid");
        	aid =  getIntent().getStringExtra("aid");
        	//titlestr =  getIntent().getStringExtra("title");
        	//hideShoucang = getIntent().getBooleanExtra("hideShoucang", false);
        	/*if( hideShoucang == true ){
        		collection_meiwen.setVisibility(View.GONE);
        	}
//        	cid = bean.getCid();
        	if(id.equals("1")){
            	title.setText("公告");
            	collection_meiwen.setVisibility(View.GONE);
            }else if(id.equals("2")){
            	title.setText("技术分享");
            }else if(id.equals("3")){
            	title.setText("美文赏析");
            }*/
        }
        layout();
        getMeiWen();
    }
    public void getMeiWen(){
    	String url = null;
    	/*if(id.equals("2")){
    		url = "http://112.124.107.39/vq_web/api.php?tab=article&act=data&aid="+aid;
//    		url = "http://112.124.107.39/vq_web/api.php?tab=article&act=data&aid="+aid+"&uid=1";
    	}else if(id.equals("3")){
    		url = "http://112.124.107.39/vq_web/api.php?tab=article&act=data&aid="+aid;
    	}*/
    	url = HHHInterface.httpHeader + "api.php?tab=article&act=data&aid="+aid;
    	HHHIml weiQuIml = new HHHIml();
    	weiQuIml.weiQuTianDiListHttp(MeiWenActivity.this, url, new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Log.e("mytag","得到的数据"+content);
						try {
							JSONObject json=new JSONObject(content);
							JSONArray array=json.getJSONArray("rows");
							JSONObject rows=array.getJSONObject(0);
							title_name.setText(rows.getString("title"));
							content_tv.setText(rows.getString("content"));
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
    }
    public void onClick(View v){
    	switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.collection_meiwen:
			if(!HhhApp.getInstance().getUname().equals("")){
				collection();
			}else{
				HhhApp.getInstance().showDialog(MeiWenActivity.this, "请先登录");
			}
			break;
		}
    }
    //收藏
    private void collection(){
    	String url = HHHInterface.httpHeader + "api.php?tab=favorite&act=add&pid="+aid+"&uid="+HhhApp.getInstance().getUid()+"&type=3";
    	weiQuIml.loginHttp(MeiWenActivity.this,url, 
    			new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				Log.e("mytag", content+"================收藏返回的数据");
				try {
					JSONObject jsonObject = new JSONObject(content);
					if(jsonObject.getBoolean("return")){
						HhhApp.getInstance().showDialog(MeiWenActivity.this, "收藏成功");
					}else if(!jsonObject.getBoolean("return")){
						HhhApp.getInstance().showDialog(MeiWenActivity.this, "已经收藏过了");
					}else{
						HhhApp.getInstance().showDialog(MeiWenActivity.this, "收藏失败");
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});	
    }
    private void layout() {
		// 头的高度
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) title
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		title.setLayoutParams(titleLayoutParams);
	}
}
